import React from "react";
import { Button, Checkbox, Form, Input } from "antd";
import axios from "../../api/axios";
import { useNavigate } from "react-router-dom";

import { username } from "../store/user";
import { useDispatch } from "react-redux";
function Index() {
  const dispatch = useDispatch();
  const naviagte = useNavigate();
  const onFinish = (values) => {
    console.log("Success:", values);
    axios.post("/login", values).then((res) => {
      console.log(res);
      localStorage.setItem("token", res.data.token);
      dispatch(username(res.data.token));
      naviagte("/home/index");
      alert("登陆成功");
      localStorage.setItem("name", res.data.username);
      localStorage.setItem(
        "avator",
        "https://img-s.msn.cn/tenant/amp/entityid/AA1ESRdi.img?w=640&h=361&m=6"
      );
    });
  };
  const onFinishFailed = (errorInfo) => {
    console.log("Failed:", errorInfo);
  };
  return (
    <div
      style={{
        width: "100vw",
        height: "100vh",
        backgroundSize: "cover",
        marginTop: "-300px",
        backgroundImage:
          "url('https://img-s.msn.cn/tenant/amp/entityid/AA1ESRdi.img?w=640&h=361&m=6')",
      }}
    >
      <div
        style={{
          width: "500px",
          height: "400px",
          backgroundColor: "#f0f2f5",
          marginTop: "300px",
          marginLeft: "35%",
          boxShadow: "0 2px 18px rgba(0, 0, 0, 0.5)",
          borderRadius: "15px",
        }}
      >
        <h2
          style={{
            textAlign: "center",
            color: "rgb(45, 187, 227)",
          }}
        >
          后台管理登录
        </h2>
        <Form
          name="basic"
          labelCol={{ span: 8 }}
          wrapperCol={{ span: 16 }}
          style={{ maxWidth: 400, marginTop: 20 }}
          initialValues={{ remember: true }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item
            label="账号"
            name="account"
            rules={[{ required: true, message: "请输入正确账号" }]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            label="密码"
            name="password"
            rules={[{ required: true, message: "请输入正确密码" }]}
          >
            <Input.Password />
          </Form.Item>

          <Form.Item
            label={null}
            style={{
              textAlign: "center",
            }}
          >
            <Button type="primary" htmlType="submit">
              确定
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
}

export default Index;
